home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir40 / progref2.zip / CHAP6 < prev    next >
Text File  |  1988-09-29  |  31KB  |  540 lines

  1. CHAPTER 6 
  2.  
  3.  Programming Technical Reference - IBM
  4.  Copyright 1988, Dave Williams
  5.  
  6.                      DOS CONTROL BLOCKS AND WORK AREAS
  7.  
  8. When DOS loads a program, it first sets aside a section of memory for the 
  9. program called the program segment, or code segment. Then it constructs a 
  10. control block called the program segment prefix, or PSP, in the first 256 
  11. (100h) bytes. Usually, the program is loaded directly after the PSP at 
  12. 100h.
  13.  The PSP contains various information used by DOS to help run the program.  
  14. The PSP is always located at offset 0 within the code segment. When a program 
  15. recieves control certain registers are set to point to the PSP. For a COM 
  16. file, all registers are set to point to the beginning of the PSP and the 
  17. program begins at 100h. For the more complex EXE file structures, only DS and 
  18. ES registers are set to point to the PSP. The linker passes the settings for 
  19. the DS, IP, SS, and SP registers and may set the starting location in CS:IP to 
  20. a location other than 100h.
  21.  
  22.  
  23.  
  24. IBMBIO provides an IRET instruction at absolute address 847h for use as a 
  25. dummy routine for interrupts that are not used by DOS. This lets the 
  26. interrupts do nothing until their vectors are rerouted to ttheir appropriate 
  27. handlers.
  28.  
  29. A storage block is used by DOS to record the amount and location of allocated 
  30. memory withion the machine's address spacd.
  31.  A storage block, a Program Segment Prefix, and an environment area are built 
  32. by FDOS for each program currently resident in the address space. The storage 
  33. block is used by DOS to record the address range of memory allocated to a 
  34. program. IOt us used by DOs to find th enext availible area to load  a program 
  35. and to determine if there is a\enough memory to run that porogram. When a 
  36. memory area is in use, it is said to be allocated. Then the program ends, or 
  37. releases memory, it is said to bne deallocated. 
  38.  A storage block contains a pointer ro rhe Program Segment Prefix assoiciated 
  39. with each program. This control block is constructed by IBMDOS for the purpose 
  40. opf providing stanfdardized areas for DOS/program communication., Within ghr 
  41. PSP are arsas which  are used to save interrupt vectors, pass parameters to 
  42. the program, record disk directory information, and to buffer disk reads and 
  43. writes. This control block is 100h bytes in lengrth and is followed by the 
  44. program mopdule loaded by DOS. 
  45.  The PSP contains a pointer to the environment area for that program. This 
  46. area contains a copy of the current DOS SET, PROMPT, COMSPEC, and PATH values 
  47. as well as any user-set variables. The program may examine and modify this 
  48. information as desired. 
  49.  Each storage block is 10h bytes long, although only 5 bytes are currently 
  50. used by DOS. The first byte contains 4Dh (a capital M) to indicate that it 
  51. contains a pointer to the next storage block. A 5Ah (a capital Z) in the 
  52. first byte of a storage block indicatres there are no more storage blocks 
  53. following this one (it is the end of the chain). The identifier byte is 
  54. followeed by a 2 byte segment number for the associated PSP for that program. 
  55. The next 2 bytes contain the number of segments what are allocated to the 
  56. program. If this is not the last storage block, then another storage block 
  57. follows the allocated memory area.
  58.  When thge storage block contains zero for the nuymber of allocated segments, 
  59. then no storage is allocated to thius block and the next storage block 
  60. immediately follows this one. This can ha-p[en whjen memory is allocated and 
  61. then deallocated repeatedly.
  62.  IBMDOS constructs a storage block and PSP before loading the command 
  63. interpreter (default is COMMAND.COM).
  64.  
  65.  
  66. If the copy of COMMAND.COM is a secondary copy, it will lack an environment 
  67. address as PSP+2Ch.
  68.  
  69.  
  70.  
  71.  The Disk Transfer Area (DTA)
  72.  
  73.  DOS uses an area in memory to contain the data for all file reads and writes 
  74. that are performed with FCB function calls. This are is known as the disk 
  75. transfer area. This disk transfer area (DTA) is sometimes called a buffer. 
  76. It can be located anywhere in the data area of your application program and 
  77. should be set by your program.
  78.  
  79.  Only one DTA can be in effect at a time, so your program must tell DOS what 
  80. memory location to use before using any disk read or write functions. Use 
  81. function call 1Ah (Set Disk Transfer Address) to set the disk transfer address.
  82. Use function call 2Fh (Get Disk Transfer Address) to get the disk transfer 
  83. address. Once set, DOS continues to use that area for all disk operations until
  84. another function call 1Ah is issued to define a new DTA. When a program is given
  85. control by COMMAND.COM, a default DTA large enough to hold 128 bytes is 
  86. established at 80h into the program's Program Segment Prefix.
  87.  
  88.  For file reads and writes that are performed with the extended function calls,
  89. there is no need to set a DTA address. Instead, specify a buffer address when 
  90. you issue the read or write call.
  91.  
  92.  
  93. DOS Program Segment
  94.  
  95.  When you enter an external command or call a program through the EXEC function 
  96. call, DOS determines the lowest availible address space to use as the start of 
  97. available memory for the program being started. This area is called the Program
  98. Segment.
  99.  At offset 0 within the program segment, DOS builds the Program Segment Prefix 
  100. control block. EXEC loads the program after the Program Segment Prefix (at
  101. offset 100h) and gives it control.
  102.  The program returns from EXEC by a jump to offset 0 in the Program Segment 
  103. Prefix, by issuing an int 20h, or by issuing an int 21h with register AH=00h or 
  104. 4Ch, or by calling location 50h in the PSP with AH=00h or 4Ch.
  105.  It is the responsibility of all programs to ensure that the CS register 
  106. contains the segment address of the Program Segment Prefix when terminating by
  107. any of these methods except call 4Ch.
  108.  
  109.  All of these methods result in returning to the program that issued the EXEC. 
  110. During this returning process, interrupt vectors 22h, 23h, and 24h (Terminate, 
  111. Ctrl-Break, and Critical Error Exit addresses) are restored from the values 
  112. saved in the PSP of the terminating program. Control is then given to the 
  113. terminate address.
  114.  
  115.  
  116. When a program receives control, the following conditions are in effect:
  117.  
  118. For all programs:
  119.  
  120. 1) The segment address of the passed environment is contained at offset 2Ch in 
  121.    the Program Segment Prefix.
  122.  
  123. 2) The environment is a series of ASCII strings totalling less than 32k bytes
  124.    in the form:       NAME=parameter      The default environment is 160 bytes.
  125.     Each string is terminated by a byte of zeroes, and the entire set of strings
  126.    is terminated by abother byte of zeroes. Following the byte of zeroes that 
  127.    terminates the set of environment string is a set of initial arguments passed
  128.    to a program that contains a word count followed by an ASCIIZ string. The 
  129.    ASCIIZ string contains the drive, path, and filename.ext of the executable 
  130.    program. Programs may use this area to determine where the program was loaded
  131.    from. The environment built by the command processor (and passed to all 
  132.    programs it invokes) contains a COMSPEC=string at a minimum (the parameter on
  133.    COMSPEC is the path used by DOS to locate COMMAND.COM on disk). The last PATH
  134.    and PROMPT commands issued will also be in the environment, along with any 
  135.    environment strings entered through the SET command. 
  136.     The environment that you are passed is actually a copy of the invoking 
  137.    process's environment. If your application terminates and stays resident 
  138.    through int 27h, you should be aware that the copy of the environment passed 
  139.    to you is static. That is, it will not change even if subsequent PATH,
  140.    PROMPT, or SET commands are issued.
  141.  
  142.    The environment can be used to transfer information between processes or to
  143.    store strings for later use by application programs. The environment is
  144.    always located on a paragraph boundary. This is its format:
  145.         byte    ASCIIZ string 1
  146.         byte    ASCIIZ string 2
  147.             ....
  148.         byte    ASCIIZ string n
  149.         byte    of zeros (0)
  150.    Typically the environment strings have the form:
  151.         parameter = value
  152.    Following the byte of zeros in the environment, a WORD indicates the number 
  153.    of other strings following. 
  154.  
  155.    If the environment is part of an EXECed command interpreter, it is followed 
  156.    by a copy of the DS:DX filename passed to the child process. A zero value 
  157.    causes the newly created process to inherit the parent's environment.
  158.  
  159. 3) Offset 80h in the PSP contains code to invoke the DOS function dispatcher.
  160.    Thus, by placing the desired function number in AH, a program can issue a
  161.    long call to PSP+50h to invoke a DOS function rather than issuing an int 21h.
  162.  
  163. 4) The disk transfer address (DTA) is set to 80h (default DTA in PSP).
  164.  
  165. 5) File Control Blocks 5Ch and 6Ch are formatted from the first two parameters 
  166.    entered when the command was invoked. Note that if either parameter contained
  167.    a path name, then the corresponding FCB will contain only a valid drive
  168.    number. The filename field will not be valid.
  169.  
  170. 6) An unformatted parameter area at 81h contains all the characters entered
  171.    after the command name (including leading and imbedded delimiters), with 80h
  172.    set to the number of characters. If the <, >, or | parameters were entered
  173.    on the command line, they (and the filenames associated with them) will not
  174.    appear in this area, because redirection of standard input and output is
  175.    transparent to applications.
  176.  
  177. (For EXE files only)
  178. 7) DS and ES registers are set to point to the PSP.
  179.  
  180. 8) CS, IP, SS, and SP registers are set to the values passed by the linker.
  181.  
  182. (For COM files only)
  183. 9) For COM files, offset 6 (one word) contains the number of bytes availible in 
  184.    the segment.
  185.  
  186. 10) Register AX reflects the validity of drive specifiers entered with the
  187.     first two parameters as follows:
  188.         AL=0FFh is the first parameter contained an invalid drive specifier,
  189.                 otherwise AL=00h.
  190.         AL=0FFh if the second parameter contained an invalid drive specifier, 
  191.                 otherwise AL=00h.
  192.  
  193. 11) All four segment registers contain the segment address of the inital 
  194.     allocation block, that starts within the PSP control block. All of user
  195.     memory is allocated to the program. If the program needs to invoke another
  196.     program through the EXEC function call (4Bh), it must first free some memory
  197.     through the SETBLOCK function call to provide space for the program being
  198.     invoked.
  199.  
  200. 12) The Instruction Pointer (IP) is set to 100h.
  201.  
  202. 13) The SP register is set to the end of the program's segment. The segment size
  203.     at offset 6 is rounded down to the paragraph size.
  204.  
  205. 14) A word of zeroes is placed on top of the stack.
  206.  
  207.  
  208.  The PSP (with offsets in hexadecimal) is formatted as follows:
  209.  
  210. ┌──────────────────────────────────────────────────────────────────────────────┐
  211. │     P  R  O  G  R  A  M       S  E  G  M  E  N  T       P  R  E  F  I  X     │
  212. ├───────┬──────────┬───────────────────────────────────────────────────────────┤
  213. │ offset│   size   │                     C O N T E N T S                       │
  214. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  215. │ 0000h │ 2 bytes  │ int 20h                                                   │
  216. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  217. │ 0002h │ 2 bytes  │ segment address, end of allocation block                  │
  218. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  219. │ 0004h │ 1 byte   │ reserved, normally 0                                      │
  220. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  221. │ 0005h │ 5 bytes  │ long call to MSDOS function dispatcher                    │
  222. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  223. │ 000Ah │ 4 bytes  │ previous termination handler interrupt vector (int 22h)   │
  224. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  225. │ 000Eh │ 4 bytes  │ previous contents of ctrl-C interrupt vector (int 23h)    │
  226. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  227. │ 0012h │ 4 bytes  │ prev. critical error handler interrupt vector (int 24h)   │
  228. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  229. │ 0016h │ 22 bytes │ reserved for DOS                                          │
  230. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  231. │ 002Ch │ 2 bytes  │ segment address of environment block                      │
  232. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  233. │ 002Eh │ 34 bytes │ reserved, DOS work area                                   │
  234. └───────┼──────────┼───────────────────────────────────────────────────────────┤
  235.         │  4 bytes │  stores the calling process's stack pointer when          │
  236.         │          │  switching to DOS's internal stack.                       │
  237. ┌───────┼──────────┼───────────────────────────────────────────────────────────┤
  238. │ 0050h │  3 bytes │ int 21h, RETF instructions                                │
  239. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  240. │ 0053h │  2 bytes │ reserved                                                  │
  241. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  242. │ 0055h │  7 bytes │ reserved, or FCB#1 extension                              │
  243. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  244. │ 005Ch │          │ default File Control Block #1                             │
  245. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  246. │ 006Ch │          │ default File Control Block #2 (overlaid if FCB #1 opened) │
  247. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  248. │ 0080h │  1 byte  │ parameter length                                          │
  249. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  250. │ 0081h │          │ parameters                                                │
  251. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  252. │ 00FFh │ 128 bytes│ command tail and default Disk Transfer Area (DTA)         │
  253. └───────┴──────────┴───────────────────────────────────────────────────────────┘
  254.  
  255. 1. The first segment of availible memory is in segment (paragraph) form. For 
  256.    example, 1000h would respresent 64k.
  257.  
  258. 2. The word at offset 6 contains the number of bytes availible in the segment.
  259.  
  260. 3. Offset 2Ch contains the segment address of the environment.
  261.  
  262. 4. Programs must not alter any part of the PSP below offset 5Ch.
  263.  
  264. offset 0 contains hex bytes CD 20, the int 20h opcode. A program can end
  265.        by making a jump to this location when the CS points to the PSP.
  266.        For normal cases, int 21, function 4Ch should be used.
  267.  
  268. offset 2 contains the segment-paragraph address of the end of memory as 
  269.         reported by DOS. (which may not be the same as the real end of RAM).
  270.         Multiply this number by 10h or 16 to get the amount of memory availible.
  271.  
  272. offset 4 reserved
  273.  
  274. offset 05 contains a long call to the DOS function dispatcher. Programs may 
  275.        jump to this address instead of calling int 21 if they wish. 
  276.  
  277. offsets 10, 14, 18  vectors
  278.  
  279. offset 2C is the segment:offset address of the environment for the program 
  280.        using this particular PSP.
  281.  
  282. offset 2E The DWORD at PSP+2EH is used by DOS to store the calling process's
  283.        stack pointer when switching to DOS's own private stack - at the end of
  284.        a DOS function call, SS:SP is restored from this address.
  285.  
  286. offset 50h contains a long call to the DOS function dispatcher.
  287.  
  288. offsets 5C, 65, 6C  contain FCB information for use with FCB function calls. 
  289.         The first FCB may overlay the second if it is an extended call; your
  290.         program should revector these areas to a safe place if you intend to
  291.         use them.
  292.  
  293. offset 80h and 81h contain th elength and value of parameters passed on the 
  294.        command line. 
  295.  
  296. offset FF contains the DTA
  297.  
  298.  
  299.  
  300.  
  301. STANDRD FILE CONTROL BLOCK
  302.  
  303.  The standard file control block is defined as follows, with the offsets in 
  304. decimal:
  305.  
  306. ┌──────────────────────────────────────────────────────────────────────────────┐
  307. │                 F I L E      C O N T R O L      B L O C K                    │
  308. ├───────┬──────────────────────────────────────────────────────────────────────┤
  309. │ Bytes │                           Function                                   │
  310. ├───────┼──────────────────────────────────────────────────────────────────────┤
  311. │   0   │ Drive number. For example:                                           │
  312. │       │ Before open:    00h = default drive                                  │
  313. │       │                 01h = drive A:                                       │
  314. │       │                 02h = drive B: etc.                                  │
  315. │       │ After open:     00h = drive C:                                       │
  316. │       │                 01h = drive A:                                       │
  317. │       │                 02h = drive B: etc.                                  │
  318. │       │ 0 is replaced by the actual drive number during open.                │
  319. ├───────┼──────────────────────────────────────────────────────────────────────┤
  320. │  1-8  │ Filename, left justified with trailing blanks. If a reserved device  │
  321. │       │ name is placed here (such as PRN) do not include the optional colon. │
  322. ├───────┼──────────────────────────────────────────────────────────────────────┤
  323. │  9-11 │ Filename extension, left justified with trailing blanks.             │
  324. ├───────┼──────────────────────────────────────────────────────────────────────┤
  325. │ 12-13 │ Current block number relative to beginning of file, starting with 0  │
  326. │       │ (set to 0 by the open function call). A block consists of 128        │
  327. │       │ records, each of the size specified in the logical record size field.│
  328. │       │ The current block number is used with the current record field       │
  329. │       │ (below) for sequential reads and writes.                             │
  330. ├───────┼──────────────────────────────────────────────────────────────────────┤
  331. │ 14-15 │ Logical record size in bytes. Set to 80h by the OPEN function call.  │
  332. │       │ If this is not correct, you must set the value because DOS uses it   │
  333. │       │ to determine the proper locations in the file for all disk reads and │
  334. │       │ writes.                                                              │
  335. ├───────┼──────────────────────────────────────────────────────────────────────┤
  336. │ 16-19 │ File size in bytes. In this 2 word field, the first word is the      │
  337. │       │ low-order part of the size.                                          │
  338. ├───────┼──────────────────────────────────────────────────────────────────────┤
  339. │ 20-21 │ Date the file was created or last updated. The mm/dd/yy are mapped   │
  340. │       │ as follows:                                                          │
  341. │       │         15  14  13  12  11  10  9  8  7  6  5  4  3  2  1  0         │
  342. │       │         y   y   y   y   y   y   y  m  m  m  m  d  d  d  d  d         │
  343. │       │ where:            mm is 1-12                                         │
  344. │       │                   dd is 1-31                                         │
  345. │       │                   yy is 0-119 (1980-2099)                            │
  346. ├───────┼──────────────────────────────────────────────────────────────────────┤
  347. │ 22-31 │ Reserved for system use.                                             │
  348. ├───────┼──────────────────────────────────────────────────────────────────────┤
  349. │  32   │ Current relative record number (0-127) within the current block.     │
  350. │       │ (See above). You must set this field before doing sequential         │
  351. │       │ read/write operations to the diskette. This field is not initialized │
  352. │       │ by the open function call.                                           │
  353. │       │  If the record size is less than 64 bytes, both words are used.      │
  354. │       │ Otherwise, only the first 3 bytes are used. Note that if you use the │
  355. │       │ File Control Block at 5Ch in the program segment, the last byte of   │
  356. │       │ the FCB overlaps the first byte of the unformatted parameter area.   │
  357. └───────┴──────────────────────────────────────────────────────────────────────┘
  358.  
  359. note 1) An unopened FCB consists of the FCB prefix (if used), drive number, and 
  360.         filename.ext properly filled in. An open FCB is one in which the 
  361.         remaining fields have been filled in by the CREAT or OPEN function 
  362.         calls.
  363.      2) Bytes 0-5 and 32-36 must be set by the user program. Bytes 16-31 are set
  364.         by DOS and must not be changed by user programs.
  365.      3) All word fields are stored with the least significant byte first. For 
  366.         example, a record length of 128 is stored as 80h at offset 14, and 00h 
  367.         at offset 15.
  368.  
  369.  
  370.  
  371. EXTENDED FILE CONTROL BLOCK
  372.  
  373.  The extended file control block is used to create or search for files in the 
  374. disk directory that have special attributes.
  375.  
  376. It adds a 7 byte prefix to the FCB, formatted as follows:
  377.  
  378. ┌──────────────────────────────────────────────────────────────────────────────┐
  379. │       E X T E N D E D     F I L E      C O N T R O L      B L O C K          │
  380. ├───────┬──────────────────────────────────────────────────────────────────────┤
  381. │ Bytes │                           Function                                   │
  382. ├───────┼──────────────────────────────────────────────────────────────────────┤
  383. │   0   │ Flag byte containing 0FFh to indicate an extended FCB.               │
  384. ├───────┼──────────────────────────────────────────────────────────────────────┤
  385. │  1-6  │ Reserved                                                             │
  386. ├───────┼──────────────────────────────────────────────────────────────────────┤
  387. │  6-7  │ Attribute byte. Refer to function call 11h (search first) for        │
  388. │       │ details on using the attribute bits during directory searches. This  │
  389. │       │ function is present to allow applications to define their own files  │
  390. │       │ as hidden (and thereby excluded from normal directory searches) and  │
  391. │       │ to allow selective directory searches.                               │
  392. └───────┴──────────────────────────────────────────────────────────────────────┘
  393.                                      
  394.  Any reference in the DOS function calls to an FCB, whether opened or unopened,
  395. may use either a normal or extended FCB. If you are using an extended FCB, the 
  396. appropriate register should be set to the first byte of the prefix, rather than
  397. the drive-number field.
  398.  
  399.  
  400.  
  401. MEMORY CONTROL BLOCKS
  402.  
  403.  DOS keeps track of allocated and availible memory blocks, and provides three 
  404. function calls for application programs to communicate their memory needs to 
  405. DOS. These calls are 48h to allocate a memory block, 49h to free a previously 
  406. allocated memory block, and 4Ah (SETBLOCK) to change the size of an allocated 
  407. memory block.
  408.  
  409.  
  410. CONTROL BLOCK
  411.  
  412. DOS manages memory as follows:
  413.  
  414.  DOS build a control block for each block of memory, whether free or allocated.
  415. For example, if a program issues an "allocate" (48h), DOS locates a block of
  416. free memory that satisfies the request, and then "carves" the requested memory 
  417. out of that block. The requesting program is passed the location of the first 
  418. byte of the block that was allocated for it - a memory management control block,
  419. describing the allocated block, has been built for the allocated block and a 
  420. second memory management control block describes the amount of space left in the
  421. original free block of memory. When you do a setblock to shrink an allocated 
  422. block, DOS builds a memory management control block for the area being freed and
  423. adds it to the chain of control blocks. Thus, any program that changed memory 
  424. that is not allocated to it stands a chance of destroying a DOS memory 
  425. management control block. This causes unpredictable results that don't show up 
  426. until an activity is performed where DOS uses its chain of control blocks. The 
  427. normal result is a memory allocation error, which means a system reset will be 
  428. required.
  429.  
  430.  When a program (command or application program) is to be loaded, DOS uses the 
  431. EXEC function call 4Bh to perform the loading.
  432.  
  433.  This is the same function call that is availible to applications programs for 
  434. loading other programs. This function call has two options:
  435.  
  436.       Function 00h, to load and execute a program (this is what the command
  437.                     processor uses to load and execute external commands)
  438.  
  439.       Function 03h, to load an overlay (program) without executing it.
  440.  
  441.  Although both functions perform their loading in the same way (relocation is 
  442. performed for EXE files) their handling of memory management is different.
  443.  
  444.  
  445. FUNCTION 0: For function 0 to load and execute a program, EXEC first allocates 
  446. the largest availible block of memory (the new program's PSP will be at offset 
  447. 0 in that block). Then EXEC loads the program. Thus, in most cases, the new 
  448. program owns all the memory from its PSP to the end of memory, including memory
  449. occupied by the transient parent of COMMAND.COM. If the program were to issue 
  450. its own EXEC function call to load and execute another program, the request 
  451. would fail because no availible memory exists to load the new program into.
  452.  
  453. NOTE: For EXE programs, the amount of memory allocated is the size of the 
  454.       program's memory image plus the value in the MAX_ALLOC field of the file's
  455.       header (offset 0Ch, if that much memory is availible. If not, EXEC 
  456.       allocates the size of the program's memory image plus the value in the 
  457.       MIN_ALLOC field in the header (offset 0Ah). These fields are set by the 
  458.       Linker).
  459.  
  460.  A well-behaved program uses the SETBLOCK function call when it receives 
  461. control, to shrink its allocated memory block down to the size it really needs.
  462. A COM program should remember to set up its own stack before doing the SETBLOCK,
  463. since it is likely that the default stack supplied by DOS lies in the area of 
  464. memory being used. This frees unneeded memory, which can be used for loading 
  465. other programs.
  466.  
  467.  If the program requires additional memory during processing, it can obtain 
  468. the memory using the allocate function call and later free it using the free 
  469. memory function call.
  470.  
  471.  When a program is loaded using EXEC function call 00h exits, its initial 
  472. allocation block (the block beginning with its PSP) is automatically freed 
  473. before the calling program regains control. It is the responsibility of all 
  474. programs to free any memory they allocate before exiting to the calling 
  475. program.
  476.  
  477.  
  478.  FUNCTION 3: For function 3, to load an overlay, no PSP is built and EXEC 
  479. assumes the calling program has already allocated memory to load the new program
  480. into - it will NOT allocate memory for it. Thus the calling program should 
  481. either allow for the loading of overlays when it determines the amount of memory
  482. to keep when issuing the SETBLOCK call, or should initially free as much memory 
  483. as possible. The calling program should then allocate a block (based on the size
  484. of the program to be loaded) to hold the program that will be loaded using the 
  485. "load overlay" call. Note that "load overlay" does not check to see if the 
  486. calling program actually owns the memory block it has been instructed to load 
  487. into - it assumes the calling program has followed the rules. If the calling 
  488. program does not own the memory into which the overlay is being loaded, there is
  489. a chance the program being loaded will overlay one of the control blocks that 
  490. DOS uses to keep track of memory blocks.
  491.  
  492.  Programs loaded using function 3 should not issue any SETBLOCK calls since
  493. they don't own the memory they are operating in. (This memory is owned by the
  494. calling program)
  495.  
  496.  Because programs loaded using function 3 are given control directly by (and 
  497. return contrrol directly to) the calling program, no memory is automatically 
  498. freed when the called program exits. It is up to the calling program to 
  499. determine the disposition of the memory that had been occupied by the exiting 
  500. program. Note that if the exiting program had itself allocated any memory, it 
  501. is responsible for freeing that memory before exiting.
  502.  
  503.  
  504.  MEMORY CONTROL BLOCKS
  505.  
  506.  Only the first 5 bytes of the memory control block are used. The first byte 
  507. will always have the value of 4Dh or 5Ah. The value 5Ah indicates the block is 
  508. the last in a chain, all memory above it is unused. 4Dh means that the block is
  509. intermediate in a chain, the memory above it belongs to the next program or to 
  510. DOS.
  511.  
  512.  The next two bytes hold the PSP segment address of the program that owns the 
  513. corresponding block of memory. A value of 0 means the block is free to be 
  514. claimed, any other value represents a segment address. Bytes 3 and 4 indicate 
  515. the size in paragraphs of the memory block. If you know the address of the first
  516. block, you can find the next block by adding the length of the memory block plus
  517. 1 to the segment address of the control block. 
  518.  
  519.  Finding the first block can be difficult, as this varies according to the DOS 
  520. version and the configuration. 
  521.  
  522.  The remaining 11 bytes are not currently used by DOS, and may contain "trash" 
  523. characters left in memory from previous applications.
  524.  
  525.   If DOS determines that the allocation chain of memory control blocks has been 
  526. corrupted, it will halt the system and display the message "Memory Allocation 
  527. Error", and the system will halt, requiring a reboot.
  528.  
  529.  Each memory block consists of a signature byte (4Dh or 5Ah) then a word which
  530. is the PSP value of the owner of the block (which allocated it), followed by a 
  531. word which is the size in paragraphs of the block. The last block has a 
  532. signature of 5Ah. All others have 4Dh. If the owner is 0000 then the block is 
  533. free.
  534.  
  535.  User memory is allocated from the lowest end of available memory that will 
  536. satisfy the request for memory.
  537.  
  538.  
  539.  
  540.